<?php
include "interface.php";
class paomeWebIndexController extends interfaceController
{
    public function __construct()
    {
        parent::__construct('show');
        $this->in_user();
    }

    public function doinit() {}

    /**
     * 俱乐部详情
     */
    public function dogetClubDetail() {
        $clubId = intval(common::request('clubId','G'));
        $uid = $this->uid;
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/club-details');
    }
   
    public function doClubList() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('uid', $uid);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/club-list');   
    }
    public function doClubFound() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        if ($uid) {
            //查询用户是否创建了俱乐部
            if($this->load('paome/paomeClub')->isCreateClubForUid($uid)){
                header('HTTP/1.1 302 Moved Permanently');
                header('Location: '."http://".$this->config['host'].'/paome_webIndex/paoExpect');exit;
            }
        }
        $this->view->assign('uid', $uid);
        $msgNoReadNum = 0;//未读消息数量
        if ($uid) {
            $msgNoReadNum = $this->load('paome/messagePm')->getMsgNum($uid, 0);
        }
        $this->view->assign('msgNoReadNum', $msgNoReadNum);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/club-found');
    }
    public function doclubDetailNo() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('uid', $uid);
        $this->view->assign('clubId', $clubId);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/club-details-no');
    }
    public function dopaomeRank() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        
        $msgNoReadNum = 0;//未读消息数量
        $isTop = 1;   //是否顶部开启引导分享(0:开启;1:不开启)
        $date = date('Ymd');
        if ($uid) {
            $msgNoReadNum = $this->load('paome/messagePm')->getMsgNum($uid, 0);

            //查询用户是否创建或加入了俱乐部
            $isJoinClub = FALSE;
            $isCreateClub = $this->load('paome/paomeClub')->isCreateClubForUid($uid);
            !$isCreateClub && $isJoinClub = $this->load('paome/paomeClub')->getUserCurrentClubInfo($uid, 'id');
            //获取当前赛季
            $season = $this->load("paome/paomeHall")->getCurrentSeason();
            if(!empty($season) && ($season['status'] > 0 && $season['status'] < 120)){
                if($isCreateClub || $isJoinClub){
                    $isTop = $this->redis->get(REDIS_PREFIX . "paomeIsTop_" . $date . "_uid_" . $uid);
                    $isTop = $isTop ? $isTop : 0;
                }
            }
        }
        $this->view->assign('uid',$uid);
        $this->view->assign('isTop', $isTop);
        $this->view->assign('msgNoReadNum', $msgNoReadNum);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/paome-rank');
    }
    public function dopaomeRankBe() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        
        $msgNoReadNum = 0;//未读消息数量
        $isTop = 1;   //是否顶部开启引导分享(0:开启;1:不开启)
        $date = date('Ymd');
        if ($uid) {
            $msgNoReadNum = $this->load('paome/messagePm')->getMsgNum($uid, 0);

            //查询用户是否创建或加入了俱乐部
            $isJoinClub = FALSE;
            $isCreateClub = $this->load('paome/paomeClub')->isCreateClubForUid($uid);
            !$isCreateClub && $isJoinClub = $this->load('paome/paomeClub')->getUserCurrentClubInfo($uid, 'id');
            //获取当前赛季
            $season = $this->load("paome/paomeHall")->getCurrentSeason();
            if(!empty($season) && ($season['status'] > 0 && $season['status'] < 120)){
                if($isCreateClub || $isJoinClub){
                    $isTop = $this->redis->get(REDIS_PREFIX . "paomeIsTop_" . $date . "_uid_" . $uid);
                    $isTop = $isTop ? $isTop : 0;
                }
            }
        }
        $this->view->assign('uid',$uid);
        $this->view->assign('isTop', $isTop);
        $this->view->assign('msgNoReadNum', $msgNoReadNum);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/paome-rank-before');
    }
    public function dopaomeHome() {
        $uid = $this->uid;
        $isInClub = FALSE;
        $isTop = 1;   //是否顶部开启引导分享(0:开启;1:不开启)
        $date = date('Ymd');
        if($uid) {
            //查询用户是否创建或加入了俱乐部
            $isJoinClub = FALSE;
            $isCreateClub = $this->load('paome/paomeClub')->isCreateClubForUid($uid);
            !$isCreateClub && $isJoinClub = $this->load('paome/paomeClub')->getUserCurrentClubInfo($uid, 'id');
            ($isCreateClub || $isJoinClub) && $isInClub = TRUE;
            //获取当前赛季
            $season = $this->load("paome/paomeHall")->getCurrentSeason();
            if(!empty($season) && ($season['status'] > 0 && $season['status'] < 120)){
                if($isCreateClub || $isJoinClub){
                    $isTop = $this->redis->get(REDIS_PREFIX . "paomeIsTop_" . $date . "_uid_" . $uid);
                    $isTop = $isTop ? $isTop : 0;
                }
            }
        }

        $msgNoReadNum = 0;//未读消息数量
        if ($uid) {
            $msgNoReadNum = $this->load('paome/messagePm')->getMsgNum($uid, 0);
        }
        $this->view->assign('msgNoReadNum', $msgNoReadNum);

        $this->view->assign('uid', $uid);
        $this->view->assign('isTop', $isTop);
        $this->view->assign('isInClub', $isInClub);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/paome-home');
    }
    public function dopaoShare() {
        $clubId = intval(common::request('clubId','G'));
        $uid = $this->uid;
        $this->view->assign('uid', $uid);
        $this->view->assign('clubId', $clubId);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/paome-share');
    }
    public function dopaoExpect() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/paome-expect');
    }
    public function dopaoDetail() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/pao-detail');
    }
    public function domeCenter() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid ? $this->uid : 0;
         $isInClub = FALSE;
         $isMasklayer = 1;   //是否开启遮罩层(0:开启;1:不开启)
         $isTop = 1;   //是否顶部开启引导分享(0:开启;1:不开启)
         $date = date('Ymd');
        if($uid) {
            //查询用户是否创建或加入了俱乐部
            $isJoinClub = FALSE;
            $isCreateClub = $this->load('paome/paomeClub')->isCreateClubForUid($uid);
            !$isCreateClub && $isJoinClub = $this->load('paome/paomeClub')->getUserCurrentClubInfo($uid, 'id');
            ($isCreateClub || $isJoinClub) && $isInClub = TRUE;
            $isMasklayer = $this->redis->get(REDIS_PREFIX . 'isMasklayer' . $uid);
            $isMasklayer = $isMasklayer ? $isMasklayer : 0;
            $this->redis->set(REDIS_PREFIX . 'isMasklayer' . $uid, 1);
            //获取当前赛季
            $season = $this->load("paome/paomeHall")->getCurrentSeason();
            if(!empty($season) && ($season['status'] > 0 && $season['status'] < 120)){
                if($isCreateClub || $isJoinClub){
                    $isTop = $this->redis->get(REDIS_PREFIX . "paomeIsTop_" . $date . "_uid_" . $uid);
                    $isTop = $isTop ? $isTop : 0;
                }
            }
        }

        $msgNoReadNum = 0;//未读消息数量
        if ($uid) {
            $msgNoReadNum = $this->load('paome/messagePm')->getMsgNum($uid, 0);
        }
        $this->view->assign('msgNoReadNum', $msgNoReadNum);

        $this->view->assign('uid', $uid);
        $this->view->assign('isTop', $isTop);
        $this->view->assign('isMasklayer', $isMasklayer);
        $this->view->assign('isInClub', $isInClub);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/me-center');
    }
    public function domeCenterNo() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/me-center-no');
    }
    public function dopaoLobby() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $isMasklayer = 1;   //是否开启遮罩层(0:开启;1:不开启)
        $msgNoReadNum = 0;//未读消息数量
        $isTop = 1;   //是否顶部开启引导分享(0:开启;1:不开启)
        $date = date('Ymd');
        if ($uid) {
            $msgNoReadNum = $this->load('paome/messagePm')->getMsgNum($uid, 0);
            $isMasklayer = $this->redis->get(REDIS_PREFIX . 'isMasklayer' . $uid);
            $isMasklayer = $isMasklayer ? $isMasklayer : 0;
            $this->redis->set(REDIS_PREFIX . 'isMasklayer' . $uid, 1);

            //查询用户是否创建或加入了俱乐部
            $isJoinClub = FALSE;
            $isCreateClub = $this->load('paome/paomeClub')->isCreateClubForUid($uid);
            !$isCreateClub && $isJoinClub = $this->load('paome/paomeClub')->getUserCurrentClubInfo($uid, 'id');
            //获取当前赛季
            $season = $this->load("paome/paomeHall")->getCurrentSeason();
            if(!empty($season) && ($season['status'] > 0 && $season['status'] < 120)){
                if($isCreateClub || $isJoinClub){
                    $isTop = $this->redis->get(REDIS_PREFIX . "paomeIsTop_" . $date . "_uid_" . $uid);
                    $isTop = $isTop ? $isTop : 0;
                }
            }
        }
        $this->view->assign('msgNoReadNum', $msgNoReadNum);
        $this->view->assign('isMasklayer', $isMasklayer);
        $this->view->assign('uid',$uid);
        $this->view->assign('isTop', $isTop);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/pao-lobby');
    }
    public function dohallOfFame() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('uid',$uid);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/halloffame');
    }
    public function dopaoNews() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/pao-news');
    }
    public function doremoveMember() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('clubId', $clubId);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/remove-member');
    }
    public function domeIntro() {
        $clubId = common::request('clubId','G'); 
        $content = common::request('content','G');
        $uid = $this->uid;
        $this->view->assign('clubId', $clubId);
        $this->view->assign('content', $content);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/me-intro');
    }
    public function donoticeLIst() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('clubId', $clubId);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/notice-list');
    }
    public function dopaoZs() {
        $clubId = common::request('clubId','G'); 
        $uid = $this->uid;
        $this->view->assign('clubId', $clubId);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/pao-zs');
    }
    public function donoticeHall() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('clubId', $clubId);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/notice-list -hall');
    }
    public function dosharePao() {
        $clubId = common::request('clubId','G');
        $uid = common::request('target_uid','R');    //分享者id
        $isSelf = 0;
        //判断用户是否在app中
        if(common::isInAppWebview()){
            $selfUid = $this->uid;
            if(empty($selfUid)){
                $_ENV['error']->getErrors(201);
                exit; 
            }
            $selfUid == $uid && $isSelf = 1;
            //获取用户当天点赞剩余次数
            $lightTimes = $this->load('paome/center')->getLightTimesInApp($selfUid, $uid);
        }else{
            $playInfo = isset($_COOKIE['wxpo']) ? $_COOKIE['wxpo'] : '';
            !empty($playInfo) && $playInfo = json_decode($playInfo, true);
            $openId = $playInfo ? $playInfo['openid'] : 0;
            //获取用户当天点赞剩余次数
            $lightTimes = $this->load('paome/center')->getLightTimes($openId, $uid);
        }
        $lightTimes = $lightTimes ? $lightTimes : 0;
        $remainTimes = 3 - $lightTimes;  //剩余抽奖次数
        
        //获取用户人气数据
        $uid = $uid ? $uid : 0;
        $userInfo = $this->load('paome/center')->getUserPopularity($uid);
        if(empty($userInfo)){
            $userInfo = array(
                'nickname' => $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $uid, 'nickname'),
                'header' => $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $uid, 'header'),
                'support_num' => '0',
                'diffLight' => '--',
                'rank' => '--',
                'rownum' => 0
            );
        }
        
        //人气榜(前十名)
        $sentimentList = $this->load("paome/paomeHall")->getHallOfFameList('support_num');
        $rankList = array();
        if($sentimentList){
            foreach ($sentimentList as $key => $val) {
               $rankList[$key]['nickname'] = isset($val['nickname']) ? $val['nickname'] : 'wx'.time();
               $rankList[$key]['header'] = isset($val['header']) ? $val['header'] : 'http://irun.hupu.com/static/images/4.jpg';
               $rankList[$key]['support_num'] = $val['support_num'];
            }
        }
        if($userInfo['rownum'] == 1 && count($rankList) > 1){
            $userInfo['diffLight'] = $rankList[0]['support_num'] - $rankList[1]['support_num'];
        }
        //分享链接处理
        $wxUrl = "http://".$this->config['host']."/paome_webIndex/paoshare";
        $clubInfo = $this->load('paome/paomeClub')->getUserCurrentClubInfo($uid);
        !empty($clubInfo) && $wxUrl = "http://".$this->config['host']."/paome_share/shareLink?target_uid=".$uid;
        
        $this->view->assign('clubId', $clubId);
        $this->view->assign('wxUrl', $wxUrl);
        $this->view->assign('isSelf', $isSelf);
        $this->view->assign('target_uid', $uid);
        $this->view->assign('remainTimes', $remainTimes);
        $this->view->assign('sentimentList', $sentimentList);
        $this->view->assign('userInfo', $userInfo);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/pao-share');
    }
    public function dopaoZan() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('uid', $uid);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/pao-zan');
    }
    public function dopaoAddress() {
        $clubId = common::request('clubId','G');
        $prizeId = common::request('id','R');   //奖品id
        $target_uid = common::request('target_uid','R');   //奖品id

        //判断用户是否在app中
        if(common::isInAppWebview()){
            $selfUid = $this->uid;
            if(empty($selfUid)){
                $_ENV['error']->getErrors(201);
                exit; 
            }
            $header = $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $selfUid, 'header') ? $this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $selfUid, 'header') : "http://joggers-test.qiniudn.com/tba79f85a7bea2b8a4b84bb9afdcfa2dd1.jpg";
            //查询是否已存在地址
            $sql = "select * from r_pm_share_winning where uid = {$selfUid}";
        }else{
            $playInfo = isset($_COOKIE['wxpo']) ? $_COOKIE['wxpo'] : '';
            !empty($playInfo) && $playInfo = json_decode($playInfo, true);
            $openId = 0;
            $header = "http://joggers-test.qiniudn.com/tba79f85a7bea2b8a4b84bb9afdcfa2dd1.jpg";
            if(!empty($playInfo)){
                $openId = $playInfo['openid'];
                $header = $playInfo['header'];
            }
            //查询是否已存在地址
            $sql = "select * from r_pm_share_winning where open_id = \"{$openId}\"";
        }
        $dressInfo = $this->db->getRow($sql);
        
        //获取奖品信息
        $prizeSql = "select name from r_pm_share_prize where pid = {$prizeId}";
        $prizeName = $this->db->getOne($prizeSql);
        //组装返回信息
        $winningInfo = array('header'=>$header,'prizeName'=>$prizeName,'prize'=>$prizeId,'realname'=>'','phone'=>'','provice'=>'','city'=>'','address'=>'');
        
        if($dressInfo){
            $winningInfo['realname'] = $dressInfo['realname'];
            $winningInfo['phone'] = $dressInfo['phone'];
            $winningInfo['provice'] = $dressInfo['provice'];
            $winningInfo['city'] = $dressInfo['city'];
            $winningInfo['address'] = $dressInfo['address'];
        }
        
        $this->view->assign('clubId', $clubId);
        $this->view->assign('target_uid', $target_uid);
        $this->view->assign('winningInfo', $winningInfo);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/pao-address');
    }
    public function dosupporter() {
        $clubId = common::request('clubId','G');
        $uid = $this->uid;
        $this->view->assign('clubId', $clubId);
        $this->view->assign('uid', $uid);
        $this->view->assign('staticHost', "http://".$this->config['host']);
        $this->view->display('paome/run/supporter');
    }

}